package com.xzc.mvc.controller;

import com.xzc.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 徐州城
 * @version 1.0
 */
@Controller
public class ParamController {

    @RequestMapping("/testServletAPI")
    //形参位置的request表示当前请求 (会被DispatcherServlet在底层中处理)
    public String testServletAPI(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }

    @RequestMapping("/testParam")
    public String testParam(
            @RequestParam(value = "user_param",required = false,defaultValue = "town") String username,
            String password,
            String hobby){
        //若个请求参数中出现多个同名的请求参数，可以再控制器方法的参数位置设置字符串类型或字符串数组接收此请求参数
        //若使用字符串类型的参数，最终结果为请求参数的每一个值之间使用逗号进行拼接
        System.out.println("username:"+username+",password:"+password + "hobby:"+hobby);
        return "success";
    }

    @RequestMapping("/testBean")
    public String testBean(User user){
        System.out.println(user);
        return "success";
    }

}
